#!/bin/sh

TEMP_DIR="/tmp/"
OLD_DIR=$PWD
SPUTNIK_DIR=$1

# move to $TEMP_DIR directory, to not pollute the local directory 
# with temporary files
cd $TEMP_DIR

# download and untar Kepler
#wget http://www.keplerproject.org/kepler-1.1-snapshot-20070519-0930.tar.gz
#tar xvzf kepler-1.1-snapshot-20070519-0930.tar.gz

wget http://www.keplerproject.org/kepler-1.1-snapshot-20070521-1825.tar.gz
#http://www.keplerproject.org/kepler-1.1-snapshot-20070511-1628.tar.gz
tar xvzf kepler-1.1-snapshot-20070521-1825.tar.gz

cd kepler-1.1

# configure, build and install kepler into $SPUTNIK_DIR
./configure --prefix=$SPUTNIK_DIR --launcher=cgi --without-readline --enable-lua
make
make install

# return to the sputnik-wiki directory
cd $OLD_DIR


# rename cgi to sputnik.cgi
cp $SPUTNIK_DIR/bin/cgi sputnik.cgi

# copy the lua files to where they need to be

cp *.lua $SPUTNIK_DIR/share/lua/5.1/
cp examples/*.lua $SPUTNIK_DIR/share/lua/5.1/
cp dependencies/*.lua $SPUTNIK_DIR/share/lua/5.1

# create and populate the initial data directory
$SPUTNIK_DIR/bin/lua5.1 make_data_dir.lua $SPUTNIK_DIR/etc/kepler/1.1/kepler_init.lua
$SPUTNIK_DIR/bin/lua5.1 make_config_file.lua $SPUTNIK_DIR/etc/kepler/1.1/kepler_init.lua $SPUTNIK_DIR

cp new_sputnik_config.lua $SPUTNIK_DIR/share/lua/5.1/sputnik_config.lua
cp cgilua.config.lua $SPUTNIK_DIR/etc/kepler/1.1/cgilua/config.lua
cp sputnik.lua $SPUTNIK_DIR/

